package cn.iocoder.yudao.module.eval.controller.admin.score;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.module.eval.controller.admin.score.vo.*;
import cn.iocoder.yudao.module.eval.dal.dataobject.score.EvalScoreSummaryDO;
import cn.iocoder.yudao.module.eval.service.score.EvalScoreSummaryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

@Tag(name = "管理后台 - 成绩汇总")
@RestController
@RequestMapping("/eval/score-summary")
@Validated
public class EvalScoreSummaryController {

    @Resource
    private EvalScoreSummaryService scoreSummaryService;

    @PostMapping("/create")
    @Operation(summary = "创建成绩汇总")
    @PreAuthorize("@ss.hasPermission('eval:score-summary:create')")
    public CommonResult<Long> createScoreSummary(@Valid @RequestBody EvalScoreSummarySaveReqVO createReqVO) {
        return success(scoreSummaryService.createScoreSummary(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新成绩汇总")
    @PreAuthorize("@ss.hasPermission('eval:score-summary:update')")
    public CommonResult<Boolean> updateScoreSummary(@Valid @RequestBody EvalScoreSummarySaveReqVO updateReqVO) {
        scoreSummaryService.updateScoreSummary(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除成绩汇总")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('eval:score-summary:delete')")
    public CommonResult<Boolean> deleteScoreSummary(@RequestParam("id") Long id) {
        scoreSummaryService.deleteScoreSummary(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得成绩汇总")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('eval:score-summary:query')")
    public CommonResult<EvalScoreSummaryRespVO> getScoreSummary(@RequestParam("id") Long id) {
        EvalScoreSummaryDO scoreSummary = scoreSummaryService.getScoreSummary(id);
        return success(BeanUtils.toBean(scoreSummary, EvalScoreSummaryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得成绩汇总分页")
    @PreAuthorize("@ss.hasPermission('eval:score-summary:query')")
    public CommonResult<PageResult<EvalScoreSummaryRespVO>> getScoreSummaryPage(@Valid EvalScoreSummaryPageReqVO pageReqVO) {
        PageResult<EvalScoreSummaryDO> pageResult = scoreSummaryService.getScoreSummaryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EvalScoreSummaryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出成绩汇总 Excel")
    @PreAuthorize("@ss.hasPermission('eval:score-summary:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportScoreSummaryExcel(@Valid EvalScoreSummaryPageReqVO exportReqVO,
                                        HttpServletResponse response) throws IOException {
        exportReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EvalScoreSummaryDO> list = scoreSummaryService.getScoreSummaryList(exportReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "成绩汇总.xls", "数据", EvalScoreSummaryRespVO.class,
                BeanUtils.toBean(list, EvalScoreSummaryRespVO.class));
    }

    @GetMapping("/list-by-plan")
    @Operation(summary = "根据计划ID获得成绩汇总列表")
    @Parameter(name = "planId", description = "计划ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('eval:score-summary:query')")
    public CommonResult<List<EvalScoreSummaryRespVO>> getScoreSummaryListByPlanId(@RequestParam("planId") Long planId) {
        List<EvalScoreSummaryDO> list = scoreSummaryService.getScoreSummaryListByPlanId(planId);
        return success(BeanUtils.toBean(list, EvalScoreSummaryRespVO.class));
    }

    @PostMapping("/calculate")
    @Operation(summary = "计算成绩汇总")
    @Parameter(name = "planId", description = "计划ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('eval:score-summary:calculate')")
    public CommonResult<Boolean> calculateScoreSummary(@RequestParam("planId") Long planId) {
        scoreSummaryService.calculateScoreSummary(planId);
        return success(true);
    }

    @PostMapping("/batch-calculate")
    @Operation(summary = "批量计算成绩汇总")
    @PreAuthorize("@ss.hasPermission('eval:score-summary:calculate')")
    public CommonResult<Boolean> batchCalculateScoreSummary(@Valid @RequestBody EvalScoreSummaryBatchCalculateReqVO batchReqVO) {
        scoreSummaryService.batchCalculateScoreSummary(batchReqVO.getPlanIds());
        return success(true);
    }

    @PutMapping("/review")
    @Operation(summary = "复核成绩汇总")
    @PreAuthorize("@ss.hasPermission('eval:score-summary:review')")
    public CommonResult<Boolean> reviewScoreSummary(@Valid @RequestBody EvalScoreSummaryReviewReqVO reviewReqVO) {
        scoreSummaryService.reviewScoreSummary(reviewReqVO.getId(), reviewReqVO.getReviewResult(), reviewReqVO.getReviewComments());
        return success(true);
    }

    @GetMapping("/statistics")
    @Operation(summary = "获取成绩统计数据")
    @Parameter(name = "planId", description = "计划ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('eval:score-summary:query')")
    public CommonResult<Object> getScoreSummaryStatistics(@RequestParam("planId") Long planId) {
        return success(scoreSummaryService.getScoreSummaryStatistics(planId));
    }

}
